package com.example.unisys.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class Quiz2JsonUtil {
    public static List<List<String>> getContent(MultipartFile file) {
        List<List<String>> result = new ArrayList<>();
        try {
            String filename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            Workbook wb;
            if (Objects.requireNonNull(filename).matches("^.+\\.(?i)(xlsx)$")) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheetAt(0);
            if (sheet == null) {
                return result;
            }

            // 循环Excel
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                List<String> tmp = new ArrayList<>();
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                if (row.getCell(0) != null) {
                    Cell cell = row.getCell(0);
                    cell.setCellType(CellType.STRING);
                    String questionInfo = cell.getStringCellValue();
                    tmp.add(questionInfo);
                }
                if (row.getCell(1) != null) {
                    Cell cell = row.getCell(1);
                    cell.setCellType(CellType.STRING);
                    String answera = cell.getStringCellValue();
                    tmp.add(answera);
                }
                if (row.getCell(2) != null) {
                    Cell cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    String answerb = cell.getStringCellValue();
                    tmp.add(answerb);
                }
                if (row.getCell(3) != null) {
                    Cell cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    String answerc = cell.getStringCellValue();
                    tmp.add(answerc);
                }
                if (row.getCell(4) != null) {
                    Cell cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    String answerd = cell.getStringCellValue();
                    tmp.add(answerd);
                }
                if (row.getCell(5) != null) {
                    Cell cell = row.getCell(5);
                    cell.setCellType(CellType.STRING);
                    String right = cell.getStringCellValue();
                    tmp.add(right);
                }
                //if(((String)tmp.get(6)).length()==1)
                result.add(tmp);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

//将excel转化成存试卷内容所用的json
/* json格式
[
  {
    "type": 1,
    "questionInfo": "",
    "questionId": 0,
    "marked": 0,
    "answers": [
      {
        "label": "A",
        "content": ""
      },
      {
        "label": "B",
        "content": ""
      },
      {
        "label": "C",
        "content": ""
      },
      {
        "label": "D",
        "content": ""
      }
    ],
    "correctAnswer": [
      "A"
    ],
    "myAnswer": [
      "A",
      "B",
      "C",
      "D",
      -1
    ]
  }
]

 */

    public static String getTestJson(MultipartFile file) {
        List<List<String>> res = getContent(file);
        JSONArray array = new JSONArray();
        int i = 0;
        for (List<String> ls : res) {
            JSONObject json = new JSONObject();
            json.put("type", 1);
            json.put("questionInfo", ls.get(0));
            json.put("questionId", i);
            i++;
            json.put("marked", 0);
            JSONArray answers = new JSONArray();
            JSONObject A = new JSONObject();
            A.put("label", "A");
            A.put("content", ls.get(1));
            answers.add(A);
            JSONObject B = new JSONObject();
            B.put("label", "B");
            B.put("content", ls.get(2));
            answers.add(B);
            JSONObject C = new JSONObject();
            C.put("label", "C");
            C.put("content", ls.get(3));
            answers.add(C);
            JSONObject D = new JSONObject();
            D.put("label", "D");
            D.put("content", ls.get(4));
            answers.add(D);
            json.put("answers", answers);
            json.put("correctAnswer", ls.get(5));
            json.put("myAnswer", -1);
            array.add(json);
        }
        return array.toJSONString();
    }


    public static List<List<String>> getContent2(File file) {
        List<List<String>> result = new ArrayList<>();
        try {
            //String filename = file.getOriginalFilename();
            String filename = file.getName();
            InputStream inputStream = new FileInputStream(file);
            Workbook wb;
            if (Objects.requireNonNull(filename).matches("^.+\\.(?i)(xlsx)$")) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheetAt(0);
            if (sheet == null) {
                return result;
            }

            // 循环Excel
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                List<String> tmp = new ArrayList<>();
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                if (row.getCell(0) != null) {
                    Cell cell = row.getCell(0);
                    cell.setCellType(CellType.STRING);
                    String questionInfo = cell.getStringCellValue();
                    tmp.add(questionInfo);
                }
                if (row.getCell(1) != null) {
                    Cell cell = row.getCell(1);
                    cell.setCellType(CellType.STRING);
                    String answera = cell.getStringCellValue();
                    tmp.add(answera);
                }
                if (row.getCell(2) != null) {
                    Cell cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    String answerb = cell.getStringCellValue();
                    tmp.add(answerb);
                }
                if (row.getCell(3) != null) {
                    Cell cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    String answerc = cell.getStringCellValue();
                    tmp.add(answerc);
                }
                if (row.getCell(4) != null) {
                    Cell cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    String answerd = cell.getStringCellValue();
                    tmp.add(answerd);
                }
                if (row.getCell(5) != null) {
                    Cell cell = row.getCell(5);
                    cell.setCellType(CellType.STRING);
                    String right = cell.getStringCellValue();
                    tmp.add(right);
                }
                //if(((String)tmp.get(6)).length()==1)
                result.add(tmp);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String path = "/Users/yanhong/Desktop/exam_template.xlsx";
        File file = new File(path);
        //System.out.println(file.getName());
        List<List<String>> res = getContent2(file);
        System.out.println(res.size());
        for (List<String> tmp : res) {
            for (String a : tmp)
                System.out.print(a + " ");
            System.out.println();
        }

    }


}


